package com.link.play.fit.ai.ui.adjust.adjustPage.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseFragment
import com.link.play.fit.ai.base.goToAc
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.AdjustPageFragment4Binding
import com.link.play.fit.ai.ui.adjust.adjustPage.AdjustPageModel
import com.link.play.fit.ai.ui.scanFood.ScanGuideAc
import com.link.play.fit.ai.utils.HealthUtils.convertKgToLbs
import com.link.play.fit.ai.utils.convertLbsToKg

/**
 *Create by SleepDog on 2025-03-24
 */
class AdjustPageFragment4 :
    BaseFragment<AdjustPageFragment4Binding>(AdjustPageFragment4Binding::inflate) {
    private val viewModel: AdjustPageModel by lazy {
        ViewModelProvider(requireActivity())[AdjustPageModel::class.java]
    }
    private var weightKgValue = 0
    private var weightLbsValue = 0

    //    private var recentlyGoalData: RecentlyGoalData? = null
    private var userGuide: UserGuide? = null
    private var isImperialMetric = false
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
        initListener()
        observerLiveData()
    }

    private fun observerLiveData() {
        viewModel.goalLiveData.observe(requireActivity(), {
            setData(it)
        })
    }

    @SuppressLint("SetTextI18n")
    private fun initData() {
        userGuide = viewModel.getGoalData()
        setData(viewModel.getGoal())
    }

    @SuppressLint("SetTextI18n")
    private fun initListener() {
        binding.ruleView.setOnValueChangedListener { value ->
            if (isImperialMetric) {
                userGuide?.let {
                    if (value > it.goalWeightKg) {
                        binding.gainWeightText.text = getString(R.string.Gain_weight)
                    } else if (value == it.goalWeightKg) {
                        binding.gainWeightText.text = getString(R.string.maintain)
                    } else {
                        binding.gainWeightText.text = getString(R.string.lose_weight)
                    }
                }
                binding.valuelbsText.text = "$value kg"
                viewModel.setGoalsValue(goalWeightLbs = convertKgToLbs(value), goalWeightKg = value)
            } else {
                userGuide?.let {
                    if (value > it.goalWeightLbs) {
                        binding.gainWeightText.text = getString(R.string.Gain_weight)
                    } else if (value == it.goalWeightLbs) {
                        binding.gainWeightText.text = getString(R.string.maintain)
                    } else {
                        binding.gainWeightText.text = getString(R.string.lose_weight)
                    }
                }
                binding.valuelbsText.text = "$value lbs"
                viewModel.setGoalsValue(goalWeightLbs = value, goalWeightKg = convertLbsToKg(value))
            }

        }
    }

    private fun setData(goal:Int){
        userGuide?.let {
            isImperialMetric = it.imperialMetric
            if (isImperialMetric) {
                binding.kglbsText.text = "kg"

                weightKgValue = when (goal) {
                    0 -> {
                        it.goalWeightKg + 10
                    }

                    1 -> {
                        it.goalWeightKg
                    }

                    else -> {
                        it.goalWeightKg - 10
                    }
                }
                binding.valuelbsText.text = "$weightKgValue kg"
                binding.ruleView.setValue(weightKgValue.toFloat())
                viewModel.setGoalsValue(
                    goalWeightLbs = convertKgToLbs(weightKgValue),
                    goalWeightKg = weightKgValue
                )
            } else {
                binding.kglbsText.text = "lbs"
                weightLbsValue = when (goal) {
                    0 -> {
                        it.goalWeightLbs + 10
                    }

                    1 -> {
                        it.goalWeightLbs
                    }

                    else -> {
                        it.goalWeightLbs - 10
                    }
                }
                binding.ruleView.setValue(weightLbsValue.toFloat())
                binding.valuelbsText.text = "$weightLbsValue lbs"
                viewModel.setGoalsValue(
                    goalWeightLbs = weightLbsValue,
                    goalWeightKg = convertLbsToKg(weightLbsValue)
                )
            }
            when (goal) {
                0 -> {
                    binding.gainWeightText.text = getString(R.string.Gain_weight)
                }

                1 -> {
                    binding.gainWeightText.text = getString(R.string.maintain)
                }

                2 -> {
                    binding.gainWeightText.text = getString(R.string.lose_weight)
                }
            }
        }
    }
}